using System;
using System.Collections.Generic;
using UnityEngine;

public class FSMStateController
{
    private FSMStateBase _curState;//当前状态
    public Character character;
    private bool _isEnter = false;

    private readonly List<FSMStateBase> _stateFromAnyList = new List<FSMStateBase>();

    public FSMStateController(Character character)
    {
        this.character = character;
    }

    public void SetState<T>() where T : FSMStateBase
    {
        var state = Activator.CreateInstance(typeof(T), this) as FSMStateBase;
        _isEnter = false;
        if (_curState != null)
        {
            _curState.Exit();
        }
        state.EarlyEnter();
        _curState = state;
    }

    public void UpdateState(float dt)
    {
        // foreach (FSMStateBase _state in _stateFromAnyList)
        // {
        //     if (_state == _curState)
        //         continue;

        //     if (_state.FromAny())
        //     {
        //         SetState(_state);
        //         break;
        //     }
        // }

        if (_curState != null && _isEnter == false)
        {
            _isEnter = true;
            _curState.Enter();
            _curState._Update(dt);
        }
        else if (_curState != null && _isEnter == true)
        {
            _curState._Update(dt);
        }
    }

    public void AddStateFromAny(FSMStateBase _state) {
        _stateFromAnyList.Add(_state);
    }
}